package com.cqc.picassodemo01;

import android.content.Context;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

import com.squareup.picasso.Picasso;
import com.squareup.picasso.Transformation;

import java.io.File;


public class MainActivity extends AppCompatActivity implements View.OnClickListener {


    private Context context = this;
    private String url1 = "http://desk.fd.zol-img.com.cn/t_s960x600c5/g4/M01/0D/04/Cg-4WVP_npmIY6GRAKcKYPPMR3wAAQ8LgNIuTMApwp4015.jpg";
    private Button btn1;
    private Button btn2;
    private Button btn3;
    private Button btn4;
    private Button btn5;
    private Button btn6;
    private ImageView iv;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        findViews();
        initViews();
    }

    private void findViews() {
        btn1 = (Button) findViewById(R.id.btn1);
        btn2 = (Button) findViewById(R.id.btn2);
        btn3 = (Button) findViewById(R.id.btn3);
        btn4 = (Button) findViewById(R.id.btn4);
        btn5 = (Button) findViewById(R.id.btn5);
        btn6 = (Button) findViewById(R.id.btn6);

        iv = (ImageView) findViewById(R.id.iv);
    }

    private void initViews() {
        btn1.setOnClickListener(this);
        btn2.setOnClickListener(this);
        btn3.setOnClickListener(this);
        btn4.setOnClickListener(this);
        btn5.setOnClickListener(this);
        btn6.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn1://加载网络图片
                Picasso.with(context).load(url1).into(iv);
                break;
            case R.id.btn2://加载资源图片
                Picasso.with(context).load(R.mipmap.b).into(iv);
                break;
            case R.id.btn3://加载文件图片，记得加权限 READ_EXTERNAL_STORAGE + WRITE_EXTERNAL_STORAGE
                String path = Environment.getExternalStorageDirectory().getAbsolutePath() + "/Pictures/Screenshots/a.png";
                File file = new File(path);
                Picasso.with(context).load(file).into(iv);
                break;
            case R.id.btn4://裁剪图片 centerCrop() 、centerInside()
                Picasso.with(context).load(url1).resize(300, 300).centerInside().into(iv);
                break;
            case R.id.btn5://占位图片
                Picasso.with(context).load(url1).placeholder(R.mipmap.b).error(R.mipmap.b).into(iv);
                break;
            case R.id.btn6://自定义裁剪
                Picasso.with(context).load(url1).transform(transformation).into(iv);
                break;
        }
    }


    private Transformation transformation = new Transformation() {
        @Override
        public Bitmap transform(Bitmap source) {
            //自定义裁剪source
//            Bitmap bitmap = ImageUtil.getRoundedCornerBitmap(source, 1000.0f);//圆
            Bitmap bitmap = ImageUtil.createReflectionImageWithOrigin(source);//倒影
            source.recycle();//原始图片要及时回收
            return bitmap;
        }

        //key()不可返回null
        @Override
        public String key() {
            return "rounded";
        }
    };
}








